# Copyright (c) 2020, Mopon.php
# All rights reserved.
# 文件名称： pathListFrame.php

__author__ = "lishang"
__date__ = "$2021-7-2 11:31:25$"

import wx,wx.grid
import wx.html
import picmap
import os,time

class GridData(wx.grid.PyGridTableBase):
    dbTable="path"
    _cols = ["路径","文件数量","最后更新时间"]
    _data = [
        ['/storage/emulated/0/DCIM/Camera',0,'2021.07.01','刷新,删除,打开目录']
    ]
    _highlighted = set()
    grid=None
    def __init__(self):
        wx.grid.GridTableBase.__init__(self)
        self.update()
        
    def getGrid(self,win):
        grid = wx.grid.Grid(win)
        self.grid=grid
        grid.SetTable(self)
        grid.EnableEditing(False)
        grid.EnableGridLines(True)
#        grid.SetColSizes(wx.grid.GridSizesInfo(150, []))
        for i,w in [(0,320),(1,70),(2,180)]:
            grid.SetColSize(i,w)
        grid.SetGridFrozenBorderPenWidth(0)
        return grid
        
        
    def update(self):
        self._data=[list(row) for row in picmap.db.execute("SELECT path,count,datetime(uptime,'unixepoch'),id FROM %s limit 50" % self.dbTable) ]
#        for row in self._data: 
#            row[2]=time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(row[2]))
        if self.grid : self.grid.Refresh()
        
    def GetColLabelValue(self, col):
        return self._cols[col]
    
    def GetValue(self, row, col):
        return self._data[row][col]
    
    def GetNumberRows(self):
        return len(self._data)

    def GetNumberCols(self):
        return len(self._cols)
    
    def SetValue(self, row, col, val):
        self._data[row][col] = val
        
    def set_value(self, row, col, val):
        self._highlighted.add(row)
        self.SetValue(row, col, val)

class pathListFrame(wx.Dialog):
    def __init__(self,  layout=""):
        self.layout=layout
        wx.Dialog.__init__(self, None, -1, '路径配置', size=(650, 400))
        
        
        self.data=GridData()
        grid=self.data.getGrid(self)
        
        
        
        self.Sizer = wx.BoxSizer(wx.VERTICAL)
        v_box_sizer = wx.BoxSizer(wx.HORIZONTAL)
        i=1
        for name in ['刷新','打开','删除','更新数据','新增目录']:
            v_box_sizer.Add(wx.Button(self,i,name),0,wx.EXPAND)
            i+=1
    #    v_box_sizer.Add(wx.Button(self,1,u"刷新"),0,wx.EXPAND)
        self.Bind(wx.EVT_BUTTON, self.onRowClick)
        self.Sizer.Add(v_box_sizer, 0, wx.EXPAND)
        self.Sizer.Add(grid, 1, wx.EXPAND)
          
          
    def onAdd(self, event):
        dlg = wx.DirDialog(self,u"选择文件夹",style=wx.DD_DEFAULT_STYLE)
        if dlg.ShowModal() == wx.ID_OK:
                path = dlg.GetPath()
                print(path)     #文件夹路径 
                picmap.db.execute("insert into `%s` ('path','intime') values('%s','%d')" % (self.data.dbTable,path,time.time()))
        self.data.update()
        dlg.Destroy()

    def onSearch(self,event):
        for row in self.data._data:
           count=picmap.searchPic(row[0])
           picmap.db.execute("update `%s` set `count`='%d',`uptime`='%d' where `path`='%s'" % (self.data.dbTable,count,time.time(),row[0])) 
        # self.gridRefresh()   

    def onRowClick(self,event):
        bid=event.GetId()
        sid=1
        rowsNum=len(self.data._data)
        if bid==1 and rowsNum==0:
           return self.data.update()
        if rowsNum==0 and  bid<4: return self.layout.alert("必须选中一行进行操作")
        row=self.data._data[0] if rowsNum>0 else []

        if bid==1:
            count=picmap.searchPic(row[0],True)
            picmap.db.execute("update `%s` set `count`='%d',`uptime`='%d' where `path`='%s'" % (self.data.dbTable,count,time.time(),row[0])) 
        elif bid==2:
             wx.Execute("nautilus %s" % row[0]) #打开目录      
        elif  bid==3:
              picmap.db.execute("delete from `%s` where id=%d limit 1" % ('path',sid))  
        elif  bid==4:
              self.onSearch(event)
              self.data.update()
        elif  bid==5:
             self.onAdd(event)
        pass